package com.example.demospringboot;



import com.example.demospringboot.drwaing.entity.Picker;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;

class Employee {
    String name;
    char originalDepartment;

    Employee(String name, char originalDepartment) {
        this.name = name;
        this.originalDepartment = originalDepartment;

    }

    @Override
    public String toString() {
        return "Employee{" +
                "名字是='" + name + '\'' +
                ", 旧部门=" + originalDepartment +
                '}';
    }
}
@SpringBootTest
class Drawing {

    @Test
    void contextLoads() {
        // 定义部门标识（A, B, C, D, E）
        char[] departments = {'A', 'B', 'C', 'D', 'E'};
        int numEmployeesPerDepartment = 5;

        // 创建员工列表
        List<Employee> employees = new ArrayList<>();
//        for (int i = 1; i <= 20; i++) {
//            char originalDept = departments[i % 5];  // 分配一个原始部门
//            employees.add(new Employee("Employee" + i, originalDept));
//        }
        for (int i=1;i<5;i++){
            employees.add(new Employee("哈尔滨局" + i, 'A'));
        }
        for (int i=1;i<6;i++){
            employees.add(new Employee("呼和浩特局" + i, 'B'));
        }
        for (int i=1;i<6;i++){
            employees.add(new Employee("兰州局" + i, 'C'));
        }
        for (int i=1;i<6;i++){
            employees.add(new Employee("南宁局" + i, 'D'));
        }
        for (int i=1;i<6;i++){
            employees.add(new Employee("青藏公司" + i, 'E'));
        }


        // 根据原始部门对员工进行分组
        Map<Character, List<Employee>> groupedByOriginalDepartment = new HashMap<>();
        for (Employee employee : employees) {
            groupedByOriginalDepartment.computeIfAbsent(employee.originalDepartment, k -> new ArrayList<>()).add(employee);
        }

        // 创建一个列表数组来存储重新分配后的部门员工
        List<List<Employee>> reassignedDepartments = new ArrayList<>();
        for (int i = 0; i < departments.length; i++) {
            reassignedDepartments.add(new ArrayList<>());
        }

        // 遍历每个原始部门，并将员工重新分配到其他部门
        for (Map.Entry<Character, List<Employee>> entry : groupedByOriginalDepartment.entrySet()) {
            char originalDept = entry.getKey();
            List<Employee> deptEmployees = entry.getValue();

            // 为这个部门的每个员工随机选择一个新部门（确保不是原来的部门）
            List<char[]> list = Arrays.asList(departments);
            List<Character> availableDepartments =new ArrayList<>();
            availableDepartments.add(list.get(0)[0]);
            availableDepartments.add(list.get(0)[1]);
            availableDepartments.add(list.get(0)[2]);
            availableDepartments.add(list.get(0)[3]);
            availableDepartments.add(list.get(0)[4]);
            availableDepartments.remove(Character.valueOf(originalDept));

            Collections.shuffle(availableDepartments);
            Iterator<Character> deptIterator = availableDepartments.iterator();

            for (Employee employee : deptEmployees) {
                char newDept = deptIterator.next();
                List<Employee> newDeptList = reassignedDepartments.get(newDept - 'A'); // 使用字符的ASCII值作为索引
                newDeptList.add(employee);

                // 为了确保每个新部门只有5个人，我们可以检查并移除多余的（但在这个特定情况下，由于我们已知每个部门有5个人，所以不需要）
                // 然而，为了完整性，这里还是保留了这个检查
                if (newDeptList.size() > numEmployeesPerDepartment) {
                    throw new IllegalStateException("Unexpected number of employees in department: " + newDeptList.size());
                }
            }
        }

        // 输出结果
        for (int i = 0; i < reassignedDepartments.size(); i++) {
            System.out.println("Department " + departments[i] + ":");
            for (Employee employee : reassignedDepartments.get(i)) {
                System.out.println(" - " + employee);
            }
        }
    }
}